// pages/checkLevel/checkLevel.js
var api = require('../../utils/api.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    superior:'',  //上级
    underling:[]  //下级
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    try {
      var superior = wx.getStorageSync('superior')
      if (superior) {
        that.setData({
          superior: superior
        })
      }else{
        that.getLevel();
      }
    } catch (e) {
      that.getLevel();
    }

    try {
      var underling = wx.getStorageSync('underling')
      if (underling) {
        that.setData({
          underling: underling
        })
      } else {
        that.getLevel();
      }
    } catch (e) {
      that.getLevel();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getLevel();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**
   * 获取层级
   */
  getLevel:function(){
    var that = this;

    api.get({
      url:'wxapp/user/getLevel',
      success:function(res){
        console.log(res);
        if(res.code){
          var superior = res.data.superior;
          var underling = res.data.underling;
        
          that.setData({
            superior: superior,
            underling: underling
          });

          //缓存
          wx.setStorage({
            key: "superior",
            data: superior
          })
          wx.setStorage({
            key: "underling",
            data: underling
          })
        }
      }
    })
  }
})